package com.mm.base.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 描述: MybatisPlus 设置默认新增/更新默认参数值
 *
 * @author: yanglin
 * @Date: 2021-08-27-15:31
 * @Version: 1.0
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 新增操作
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        // 属性名称，不是字段名称
        this.setFieldValByName("createBy", "admin", metaObject);
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("lastUpdateBy", "admin", metaObject);
        this.setFieldValByName("lastUpdateTime", new Date(), metaObject);
    }

    /**
     * 更新操作
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("lastUpdateBy", "admin", metaObject);
        this.setFieldValByName("lastUpdateTime", new Date(), metaObject);
    }
}
